# coding: utf-8
# 文件名称: 拉钩_spider_json.py
# 创建时间: 2021/6/27 16:45

import requests
from lxml import etree
import json
import time

headers = {
        'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36 Edg/91.0.864.54',
        'referer':'https://www.lagou.com/jobs/list_SEO?labelWords=&fromSearch=true&suginput=',
        'cookie':'RECOMMEND_TIP=true; user_trace_token=20210522103000-d0d1cbb5-d775-47cd-afc1-8e3401bbe5a2; LGUID=20210522103000-21165b5a-6739-44cb-9022-9aa0e39be43f; _ga=GA1.2.206952919.1621650601; LG_LOGIN_USER_ID=1576d7da02643b1dc4761e231f14d86438dca6b32121793569134a048cbfabb1; LG_HAS_LOGIN=1; JSESSIONID=ABAAAECABIEACCAC669615D20129A67184ED065AE4E2304; WEBTJ-ID=20210627%E4%B8%8B%E5%8D%884:47:16164716-17a4ca6c6371e7-0cac8bcbd36e02-7a697d6e-1327104-17a4ca6c638b24; sensorsdata2015session=%7B%7D; privacyPolicyPopup=false; _gid=GA1.2.1186771810.1624783637; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1623478272,1624415184,1624783637; index_location_city=%E6%B7%B1%E5%9C%B3; __lg_stoken__=ef2126fed56811dc7f18a1428d6ac53d0ce28533a1d7625ec270a63a178b99cbd6a5c04e46f13b4b8a348a456617fd7b4960dbd7fe59088dc468b2fdb54e3227a391d4921bf3; X_MIDDLE_TOKEN=82b2f100bc59309b4d0cd0a35b9c9955; TG-TRACK-CODE=search_code; _gat=1; PRE_UTM=; PRE_HOST=; PRE_LAND=https%3A%2F%2Fwww.lagou.com%2Fjobs%2F3272515.html; LGSID=20210627184705-f9d68e82-4a07-4e9d-a63b-8002214da025; PRE_SITE=; X_HTTP_TOKEN=31c33b49941aa3e2750197426124aa936cfa402d4a; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2216335208%22%2C%22first_id%22%3A%2217991e872cc782-0f4a0e1c7af33f-7e697a60-1327104-17991e872cd877%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24os%22%3A%22Windows%22%2C%22%24browser%22%3A%22Chrome%22%2C%22%24browser_version%22%3A%2291.0.4472.114%22%7D%2C%22%24device_id%22%3A%2217991e872cc782-0f4a0e1c7af33f-7e697a60-1327104-17991e872cd877%22%7D; LGRID=20210627185057-4b3b316a-447a-403f-b073-0e3c69422b9f; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1624791057; SEARCH_ID=d6ac5f53b4214c75a9643c47ab462025',
        'origin':'https://www.lagou.com',

        }

def request_list_page():
    url = 'https://www.lagou.com/jobs/positionAjax.json?city=%E6%B7%B1%E5%9C%B3&needAddtionalResult=false'

    data = {
        'first':'false',
        'pn':1,
        'kd':'SEO'
    }

    for x in range(1, 11):
        data['pn'] = x

        response = requests.post(url,headers=headers, data=data)
        text = response.text
        persons = json.loads(text)
        print(persons)

        contents = persons['content']['positionResult']['result']
        for content in contents:
            positionId = content['positionId']
            # 构建职位详情页的url
            position_url = f'https://www.lagou.com/jobs/{positionId}.html'
            print(position_url)
            parse_position_detail(position_url)
            break
        time.sleep(3)
        break

# headers = {
#     'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36 Edg/91.0.864.54',
#
# }
# 爬取详情页函数
def parse_position_detail(url):
    pass
    # 拉钩的反爬太强大，此时无法爬取成功！
    # 改用 selenium 方式进行爬取

def main():
    request_list_page()

if __name__ == '__main__':
    main()